from asyncio import AbstractEventLoop
from tqsdk import TqApi, TqAuth
import json

from tqsdk.backtest import TqBacktest, TqReplay
from tqsdk.multiaccount import TqMultiAccount
# from info import user_info

class TQapi(TqApi):
    def __init__(self, signalHandle,info,account= None, auth= None, url= None, backtest= None, web_gui=False, debug= None, loop= None, disable_print= False, _stock= True, _ins_url=None, _md_url=None, _td_url=None) -> None:
        super().__init__(account, auth, url, backtest, web_gui, debug, loop, disable_print, _stock, _ins_url, _md_url, _td_url)
    # def __init__(self, auth: TqAuth,signalHandle,info) -> None:
    #     super().__init__(auth)
    # def __init__(self,signalHandle,info) -> None:
        self.signalHandle=signalHandle
        self.closeGUI=False
        self.info=info

        
        
        self.updateGUIdata={
            "keyEnterID":None,
            "keyEnterQuote":None,
        }
        self.signalHandle.closeApiSignal.connect(self.closeApi)
        self.signalHandle.getQuoteSignal.connect(self.onGetQuote)
        # self.signalHandle.sendOrderSignal.connect()
        ##登陆账号
        # try:
        #     self.api = TqApi(auth=TqAuth(self.info['userinfo']['TQ_username'],self.info['userinfo']['TQ_password']))
        # except:
        #     raise Exception('登陆失败')
    
    def onGetQuote(self,exchange_ID):
        '''条件单准备栏，回车时，订阅品种行情'''
        # self.updateGUIdata['keyEnterID']=exchange_ID
        # self.updateGUIdata['keyEnterQuote']=self.api.get_quote(exchange_ID)
        
    def run(self):
        while not self.closeGUI :
            self.wait_update()
            self.signalHandle.updateGUISignal.emit()
        self.close()
    def closeApi(self,s):
        self.closeGUI=True